package com.wl.eb.controller;

import com.wf.captcha.utils.CaptchaUtil;
import com.wl.eb.module.EbUser;
import com.wl.eb.service.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/login")
public class LoginController {
@Resource
private LoginService loginService;

    //返回登录界面
    @GetMapping("/on")
    public  String loginPage(){
        return "login";
    }

    @PostMapping
    public  String login(EbUser ebUser,String verycode, HttpServletRequest request){
        System.out.println("==LoginController的login方法，ebUser:"+ebUser);
        //校验验证码
        if(!verycode.equals(request.getSession().getAttribute("captcha"))){
            throw new RuntimeException("验证码错误！！！");
        }
        //1.调用service进行登录
        EbUser loginUser = loginService.login(ebUser);
        //2.不绑定数据到model
        request.getSession().setAttribute("loginUser",loginUser);
        //3.返回View
        return "redirect:index/index";// 登录成功，则返回到 网站 首页
    }

    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }

}
